package white440.controller;

import java.rmi.RemoteException;

import javax.swing.JLabel;

import white440.common.RemoteTruck;

/**
 * Class to display Webcam in GUI
 * 
 * @author Stephen Panosian with edits by evan
 */

public class WebcamComponent extends JLabel implements Runnable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8853582206103293586L;
	//private ImageIcon image;
	private RemoteTruck remoteTruck;
	private int webcamID;
	
	/**
	 * Makes a new component that starts displaying live video feed imediately.
	 * @param cameraID which camera on the truck to access.
	 * 	They are numbered starting with 0.
	 * @param truck the remote truck that the camera is attached to.
	 */
	public WebcamComponent(int cameraID, RemoteTruck truck){
		super();
		webcamID = cameraID;
		remoteTruck = truck;
		(new Thread(this)).start();
	}
	
	public void run(){
		while(true) {
			try {
				//image = remoteTruck.getCameraImage(webcamID);
				
				this.setIcon(remoteTruck.getCameraImage(webcamID));
				repaint();
			} catch (RemoteException e) {
				System.out.println("Could not get image from truck");
				e.printStackTrace();
			}
			
		}
	}
}
